package com.community.zhihuishequ.view.record;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 社区档案
 */
public class RecordListActivity extends BaseActivity {

    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.rv_car_list)
    RecyclerView rvCarList;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_search)
    RelativeLayout rlSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.btn_all)
    Button btnAll;
    @BindView(R.id.btn_review)
    Button btnReview;
    @BindView(R.id.btn_into_file)
    Button btnIntoFile;
    @BindView(R.id.ll_btn_group)
    LinearLayout llBtnGroup;

    private ContentAdapter adapter;
    private List<RecordData> dataList;
    private List<RecordData> contentList;
    private int type = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_list);
        unbinder = ButterKnife.bind(this);
        initActionBar(toolBar, true);
        toolBar.setNavigationIcon(R.drawable.ic_back);
        initData();
        initList();
    }

    private void initData() {
        dataList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            RecordData recordData = new RecordData();
            recordData.setName("张三" + String.valueOf(i));
            recordData.setAddress("八步区嘉年豪庭小区9座13" + String.valueOf(i) + "房");
            recordData.setCategory(i % 2 == 0 ? "租户" : "住户");
            recordData.setStatus(i % 2 == 0 ? 0 : 1);
            dataList.add(recordData);
        }
    }

    private void search(int type, String searchString) {
        if (contentList == null) {
            contentList = new ArrayList<>();
            contentList.addAll(dataList);
        }
        List<RecordData> templateList = new ArrayList<>();
        for (RecordData recordData : contentList) {
            if (type != -1) {
                if (recordData.getStatus() != type) {
                    continue;
                }
            }
            if (!StringUtils.isEmpty(searchString)) {
                boolean isContent = false;
                if (recordData.getName().contains(searchString)) {
                    isContent = true;
                } else if (recordData.getAddress().contains(searchString)) {
                    isContent = true;
                } else if (recordData.getCategory().contains(searchString)) {
                    isContent = true;
                }
                if (!isContent) {
                    continue;
                }
            }
            templateList.add(recordData);
        }
        dataList.clear();
        adapter.notifyDataSetChanged();
        dataList.addAll(templateList);
        adapter.notifyDataSetChanged();
    }

    private void initList() {
        adapter = new ContentAdapter(R.layout.item_record, dataList);
        rvCarList.setLayoutManager(new LinearLayoutManager(this));
        rvCarList.setAdapter(adapter);
    }

    @OnClick(R.id.btn_all)
    public void onBtnAllClicked() {
        type = -1;
        search(type, etSearch.getText().toString());
        llSearch.setVisibility(View.VISIBLE);
        llBtnGroup.setVisibility(View.GONE);
        tvTitle.setText("全部");
    }

    @OnClick(R.id.btn_review)
    public void onBtnReviewClicked() {
        type = 1;
        search(type, etSearch.getText().toString());
        llSearch.setVisibility(View.VISIBLE);
        llBtnGroup.setVisibility(View.GONE);
        tvTitle.setText("待审查");
    }

    @OnClick(R.id.btn_into_file)
    public void onBtnIntoFileClicked() {
        type = 0;
        search(type, etSearch.getText().toString());
        llSearch.setVisibility(View.VISIBLE);
        llBtnGroup.setVisibility(View.GONE);
        tvTitle.setText("已入档");
    }

    @OnClick(R.id.tv_title)
    public void onTvTitleClicked() {
        llSearch.setVisibility(View.GONE);
        llBtnGroup.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.rl_search)
    public void onRlSearchClicked() {
        search(type, etSearch.getText().toString());
    }

    private class ContentAdapter extends BaseQuickAdapter<RecordData, BaseViewHolder> {

        public ContentAdapter(int layoutResId, @Nullable List<RecordData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RecordData item) {
            helper.setText(R.id.tv_name, item.getName());
            helper.setText(R.id.tv_category, "住户类型：" + item.getCategory());
            helper.setText(R.id.tv_address, "住址：" + item.getAddress());
            if (item.getStatus() == 0) {
                helper.setText(R.id.tv_status, "已入档");
                helper.setBackgroundRes(R.id.tv_status, R.drawable.bg_gray);
            } else if (item.getStatus() == 1) {
                helper.setText(R.id.tv_status, "待审查");
                helper.setBackgroundRes(R.id.tv_status, R.drawable.bg_yellow);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
